<?php

require_once(dirname(__FILE__) . '/appconf.php');

class Test extends Site {

    function setup() {
        $this->run_modes('test');
        $this->start_mode('test');

        parent::setup();
    }

    function cgiapp_prerun() {

        parent::cgiapp_prerun();
    }

    function action_test() {


        $file = "http://blog.fashionguide.com.tw/api/imore_posts.json";
        $json_array = json_decode(file_get_contents($file), 1);
        if ($json_array) {
            foreach ($json_array as $value) {

                $insert_array = array("id", "user_id", "category_id", "user_category_id", "title", "content", "post_status", "published_at", "created_at", "updated_at", "is_enabled", "user_category_name", "post_url", "user_nickname", "category_name");

                $insert = array();
                foreach ($insert_array as $insert_value) {

                    $insert[$insert_value] = $value[$insert_value];
                }
                $ret = uploadfiles($this->param('WWW') . '/upload/fg_blog', $value['mid_post_cover_url']);

                if ($ret[0]['code'] == 1) {
                    $insert['mid_post_cover_url'] = str_replace($this->param('WWW')."/", '', $ret[0]['path']);
                }

                $ret_avatar = uploadfiles($this->param('WWW') . '/upload/fg_blog', $value['user_avatar']);

                if ($ret_avatar[0]['code'] == 1) {
                    $insert['user_avatar'] = str_replace($this->param('WWW')."/", '', $ret_avatar[0]['path']);
                }


                db_add('fg_blog', $insert);
                echo $value['id'] . "<br>";
            }
        }
    }

}

header("Content-type: text/html; charset=utf-8");
$webapp = new Test();
$webapp->run();
?>